<?php

namespace app\api\controller;

use Yansongda\Pay\Pay as ysd;
use think\Db;
use app\api\controller\My;
use think\Session;
use think\Cache;
use fast\Random;
use fast\Http;

/**
 * 支付部分
 */
class Pay
{


    //微信支付签名
    public function getSign($data = array(), $url, $randstr, $time, $post)
    {
        $apiclient_cert = __DIR__ . '/cert/apiclient_cert.pem';
        $apiclient_key = __DIR__ . '/cert/apiclient_key.pem';
        $str = $post . "\n" . $url . "\n" . $time . "\n" . $randstr . "\n" . $data . "\n";
        // echo strlen($str);
        $key = file_get_contents($apiclient_key); //在商户平台下载的秘钥
        $str = $this->getSha256WithRSA($str, $key);
        return $str;
    }


    public  function getSha256WithRSA($content, $privateKey)
    {
        $binary_signature = "";
        $algo = "SHA256";
        openssl_sign($content, $binary_signature, $privateKey, $algo);
        $sign = base64_encode($binary_signature);
        return $sign;
    }
    public  function toBalance($openid = '', $transfer_amount = '', $transfer_remark = '', $out_batch_no = '', $appid = 'wx2f396da944f64fe8')
    {

        $data = [
            'appid' => $appid,
            'out_batch_no'  => $out_batch_no,       //商户订单号
            'batch_name' => date('YmdHis'),
            'transfer_detail_list' => [[
                'out_detail_no' => $out_batch_no,
                'transfer_amount' => $transfer_amount,
                'transfer_remark' => $transfer_remark,
                'openid' => $openid,                        //收款人的openid
            ]],

            'total_num' => 1,              //check_name为 FORCE_CHECK 校验实名的时候必须提交
            'total_amount' => $transfer_amount,                       //企业付款金额，单位为分
            'batch_remark' => $transfer_remark,                  //付款说明
        ];

        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
        $noncestr = Random::alpha(16);
        $timeStamp = time();
        $datajson = json_encode($data);
        $key = $this->getSign($datajson, '/v3/transfer/batches', $noncestr, $timeStamp,  'POST');

        // $response = $app->post('/v3/marketing/favor/users/' . $openid . '/coupons', $data);

        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', '1663340087', '4019A662DE6A10E6ABCB8D7EE297F7DD04DFCD5A', $noncestr, $timeStamp, $key); //头部信息
        // return $token;
        $header  = [
            'Content-Type:' . ' application/json; charset=UTF-8',
            'Accept: application/json',
            'User-Agent: */*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token,
            'Wechatpay-Serial' => '4019A662DE6A10E6ABCB8D7EE297F7DD04DFCD5A'
        ];
        $options = [
            CURLOPT_HTTPHEADER => $header
        ];
        //{"coupon_id":"55755803608"}
        return http::post($url, json_encode($data), $options);
        //{"batch_id":"131000011083401975372722023122713330820322","batch_status":"ACCEPTED","create_time":"2023-12-27T20:38:40+08:00","out_batch_no":"sdfd111111111"}
        //{"code":"PARAM_ERROR","detail":{"location":"body","value":""},"message":"无法将 JSON 输入源“/body/total_amount”映射到目标字段“转账总金额”中，此字段需要一个合法的 64 位有符号整数"}
    }
    public  function query($out_batch_no = 'TR202312280903183766672')
    {

        $data_q = [
            'need_query_detail' => 'false'
        ];
        $data = '';
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/' . $out_batch_no;
        $noncestr = Random::alpha(16);
        $timeStamp = time();
        $datajson = $data;
        $key = $this->getSign($datajson, '/v3/transfer/batches/out-batch-no/' . $out_batch_no . '?' . http_build_query($data_q), $noncestr, $timeStamp, 'GET');

        // $response = $app->post('/v3/marketing/favor/users/' . $openid . '/coupons', $data);

        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', '1663340087', '4019A662DE6A10E6ABCB8D7EE297F7DD04DFCD5A', $noncestr, $timeStamp, $key); //头部信息
        // return $token;
        $header  = [
            //'Content-Type:' . ' application/json; charset=UTF-8',
            'Accept: application/json',
            // 'User-Agent: */*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token,
            // 'Wechatpay-Serial' => '4019A662DE6A10E6ABCB8D7EE297F7DD04DFCD5A'
        ];
        $options = [
            CURLOPT_HTTPHEADER => $header
        ];
        //{"coupon_id":"55755803608"}
        return http::get($url . '?' . http_build_query($data_q), '', $options);
        //{"batch_id":"131000011083401975372722023122713330820322","batch_status":"ACCEPTED","create_time":"2023-12-27T20:38:40+08:00","out_batch_no":"sdfd111111111"}
        //{"code":"PARAM_ERROR","detail":{"location":"body","value":""},"message":"无法将 JSON 输入源“/body/total_amount”映射到目标字段“转账总金额”中，此字段需要一个合法的 64 位有符号整数"}
    }
    /**
     * 微信小账本
     */
    public function wechatXzb()
    {
        my::dingding('小账本订单2：' . json_encode(request()->request()));
        return 'ok';
    }
}
